/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-04-23
 * Time:8:06
 */
public class Demo11 {

    public static void main(String[] args) {
        Thread B = new Thread(()->{
            System.out.println("B还在赶作业，等一等马上好！");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("B终于赶完作业了……");
        });
        B.start();

        Thread A = new Thread(()->{
            System.out.println("就差B的作业了……");
            try {
                B.join(2000);//等待B完成作业，只等2秒，再晚就来不及交给老师了
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (B.isAlive()){//两秒之后，如果B还在赶作业，那A也不再等了，直接把作业交给老师了
                System.out.println("A不能再等了，现在就把作业交给老师了。");
            }else {//如果B已经写完作业了
                System.out.println("A收齐作业了，赶紧交给老师");
            }
        });
        A.start();

    }
}
